﻿/*
* 作 者: 槐尧
* 公 司: 墨宇天城
* 创建时间: 2016/3/4 10:55:39
* 说明:  
*/

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

public class FruitDataManager
{
    private static FruitDataManager intstance;
    public static FruitDataManager Intstance
    {
        get
        {
            if (intstance == null)
            {
                intstance = new FruitDataManager();
                intstance.Init();
            }
                
            return intstance;
        }
    }

    private static int m_UserCoin = 0;

    public static int UserCoin
    {
        get { return FruitDataManager.m_UserCoin; }
        set { FruitDataManager.m_UserCoin = value; }
    }

    const string dataPath = "fruitdata";
    private Dictionary<FruitEnum, FruitGiven> m_fruitGiven = new Dictionary<FruitEnum, FruitGiven>();
    private Dictionary<int, FruitData> m_fruitData = new Dictionary<int, FruitData>();
    private Dictionary<int, int> defaultPrice = new Dictionary<int, int>();

    private List<int> m_lstPresent = new List<int>();
    public void Init()
    {
        InitDefault();
    }

    /// <summary>
    /// 初始化默认数据
    /// </summary>
    public void InitDefault()
    {
        string path = Application.StartupPath + "/fruitdata";
        string content = FileManager.ReadFile(path);
        InitDate(content);
    }

    /// <summary>
    /// 根据内容初始化数据
    /// </summary>
    /// <param name="content"></param>
    void InitDate(string content)
    {
        Dictionary<string, object> dataDic = Json.Deserialize(content) as Dictionary<string, object>;
        List<object> lstFruitGiven = dataDic["givedata"] as List<object>;
        foreach (Dictionary<string, object> o in lstFruitGiven)
        {
            FruitGiven fg = new FruitGiven(o);
            m_fruitGiven[fg.iFruitid] = fg;
        }

        List<object> lstFruitData = dataDic["fruitdata"] as List<object>;
        foreach (Dictionary<string, object> o in lstFruitData)
        {
            FruitData fd = new FruitData(o);
            m_fruitData[fd.iIndex] = fd;
            if (fd.iNum != 0)
            {
                defaultPrice[fd.iIndex] = fd.iNum;
            }
            else
            {
                if (fd.iFruitId == FruitEnum.GoodLuck1)
                {
                    defaultPrice[fd.iIndex] =101;
                }
                else if(fd.iFruitId == FruitEnum.GoodLuck2)
                {
                    defaultPrice[fd.iIndex] = 102;
                }
                else
                {
                    defaultPrice[fd.iIndex] = m_fruitGiven[fd.iFruitId].iGivenNum;
                }
            }
        }

        List<object> lstPre = dataDic["fruitprecent"] as List<object>;
        foreach (int obj in lstPre)
        {
            m_lstPresent.Add(obj);
        }
    }
    public FruitGiven GetFruitGiven(FruitEnum fruidId)
    {
        return m_fruitGiven[fruidId];
    }
    public FruitData GetFruitData(int index)
    {
        return m_fruitData[index];
    }

    public int GetdefaultPrice(int index)
    {
        return defaultPrice[index];
    }

    public List<int> GetPresentLst()
    {
        return m_lstPresent;
    }


    public void Save()
    {
        string dataStr = GetSaveStr();
        string path = Application.StartupPath + "/fruitdata";
        FileManager.WriteBytes(path, dataStr);
    }

    public string GetSaveStr()
    {
        Dictionary<string, object> dic = new Dictionary<string, object>();
        List<object> lstGive = new List<object>();
        foreach (KeyValuePair<FruitEnum, FruitGiven> kv in m_fruitGiven)
        {
            lstGive.Add(kv.Value.ToDic());
        }

        List<object> lstFruitData = new List<object>();
        foreach (KeyValuePair<int, FruitData> kv in m_fruitData)
        {
            lstFruitData.Add(kv.Value.ToDic());
        }

        List<object> lstFruitPrecent = new List<object>();
        foreach (int p in m_lstPresent)
        {
            lstFruitPrecent.Add(p);
        }

        dic["givedata"] = lstGive;
        dic["fruitdata"] = lstFruitData;
        dic["fruitprecent"] = lstFruitPrecent;

        return Json.Serialize(dic);
    }
}